const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../utils/config.js'
import {
  reqApprove,
  reqProductList
} from '../../request/api'
Page({
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    skeletonLoading: true,
    rolename: '',
    token: '',
  },
  onLoad(options) {
    console.log(options, 'options');
    if (app.globalData.token && wx.getStorageSync('token')) {
      this.getProductList()
    }else{
      this.onSkeleton()
    }
    if (options && options.shareUserId || '') {
      let shareData = {
        shareUserId: options.shareUserId,
        preId: options.preId,
        homeId: options.homeId,
        nickname2: options.nickname,
        homeName: options.homeName,
        startTime: options.startTime,
        totalTime: options.totalTime,
      }
      wx.setStorageSync('shareData', shareData)
      this.isLoginShare(shareData)  // 判断登陆状态
      return;
    }
  },
  onShow() {
    this.getTabBar().init();
    if (app.globalData.shareUserId) {
      this.loginShare() // 登录后重新进来接受分享
    }
    this.setData({
      rolename: app.globalData.rolename,
      token: app.globalData.token,
      skeletonLoading: false
    })
  },
     // 产品列表
     async getProductList() {
      const res = await reqProductList()
      if (res.code !== 20000) return showMsg(res.message)
    },
  // 判断登陆状态
  isLoginShare(shareData) {
    var _this = this
    if (app.globalData.token && wx.getStorageSync('token')) {
      let time = new Date().getTime();
      if (time - shareData.startTime >= shareData.totalTime) {
        shareData.shareUserId = ''
        shareData.preId = ''
        shareData.homeId = ''
        shareData.nickname2 = ''
        shareData.homeName = ''
        shareData.startTime = ''
        shareData.totalTime = ''
        wx.showModal({
          title: '温馨提示',
          content: '链接已失效',
          showCancel: false,
          confirmText: '确定',
        })
        return;
      }
      wx.showModal({
        title: '家庭绑定',
        content: `${shareData.nickname2}邀请您加入${shareData.homeName}`,
        success: function (res) {
          if (res.confirm) {
            app.globalData.shareUserId = shareData.shareUserId
            app.globalData.preId = shareData.preId
            app.globalData.homeId = shareData.homeId
            app.globalData.nickname2 = shareData.nickname2
            app.globalData.homeName = shareData.homeName
            app.globalData.totalTime = shareData.totalTime
            app.globalData.startTime = shareData.startTime
            _this.onAddMemberApi()
          } else {
            shareData.shareUserId = ''
            shareData.preId = ''
            shareData.homeId = ''
            shareData.nickname2 = ''
            shareData.homeName = ''
            shareData.startTime = ''
            shareData.totalTime = ''
            wx.removeStorageSync('shareData')
          }
        }
      })
    } else {
      app.globalData.shareUserId = shareData.shareUserId
      app.globalData.preId = shareData.preId
      app.globalData.homeId = shareData.homeId
      app.globalData.nickname2 = shareData.nickname2
      app.globalData.homeName = shareData.homeName
      app.globalData.startTime = shareData.startTime
      app.globalData.totalTime = shareData.totalTime
      wx.showModal({
        title: '温馨提示',
        content: '请先登录后再接受分享',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }
        }
      })
    }
  },
  async onAddMemberApi() {
    let params = {
      userId: wx.getStorageSync('userId'),
      preId: app.globalData.preId,
      homeId: app.globalData.homeId,
      result: 1,
    }
    console.log(params, 'params');
    let res = await reqApprove(params)
    app.globalData.shareUserId = ''
    app.globalData.preId = ''
    app.globalData.homeId = ''
    app.globalData.nickname2 = ''
    app.globalData.homeName = ''
    app.globalData.startTime = ''
    app.globalData.totalTime = ''
    wx.removeStorageSync('shareData')
    if (res.code !== 20000) return showMsg(res.message);
    showMsg('加入家庭成功')
    // 事件总线
    app.eventBus.emit('family');
    // 通过选择器获取子组件实例
    // const child = this.selectComponent('#ordinary');
    // if (child) {
    //   child.getordinary(); // 调用子组件方法
    // }
    // this.onStudentPage()
  },
  // 登录后重新进来接受分享
  loginShare() {
    let _this = this
    if (app.globalData.token && wx.getStorageSync('token')) {
      console.log(app.globalData, "app.globalData")
      let time = new Date().getTime();
      if (time - app.globalData.startTime >= app.globalData.totalTime) {
        app.globalData.shareUserId = ''
        app.globalData.preId = ''
        app.globalData.homeId = ''
        app.globalData.nickname2 = ''
        app.globalData.homeName = ''
        app.globalData.startTime = ''
        app.globalData.totalTime = ''
        // wx.removeStorageSync('shareData')
        wx.showModal({
          title: '温馨提示',
          content: '链接已失效',
          showCancel: false,
          confirmText: '确定',
        })
        return;
      }
      wx.showModal({
        title: '家庭绑定',
        content: `${app.globalData.nickname2}邀请您加入${app.globalData.homeName}`,
        success: function (res) {
          if (res.confirm) {
            _this.onAddMemberApi()
          } else {
            app.globalData.shareUserId = ''
            app.globalData.preId = ''
            app.globalData.homeId = ''
            app.globalData.nickname2 = ''
            app.globalData.homeName = ''
            app.globalData.startTime = ''
            app.globalData.totalTime = ''
            wx.removeStorageSync('shareData')
          }
        }
      })
    }
  },
  onSkeleton() {
    if (!this.data.token) {
      Dialog.confirm({
          title: '温馨提示',
          message: '您还未登录，是否立即登录？',
        })
        .then(async () => {
          wx.redirectTo({
            url: '/pages/login/login',
          })
        })
        .catch(() => {});
    }
  }

})